package com.example.xpvideo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.xpvideo.entity.LsTreaty;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.service.LsTreatyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hjt
 * @create 2021-08-30 11:33
 * @describe：协议
 */
@RestController
@RequestMapping("treaty")
public class TreatyController {
    @Autowired
    private LsTreatyService treatyService;

    /**
     * 查询所有协议
     * @return
     */
    @GetMapping("queryTreatyAll")
    public ResponseResult queryTreatyAll() {
        List<LsTreaty> list = treatyService.list();
        return ResponseResult.responseResult(0, list);
    }

    /**
     * 根据id 查询单条协议
     * @param id
     * @return
     */
    @GetMapping("queryTreatyById/{treatyId}")
    public ResponseResult queryTreatyById(@PathVariable("treatyId") Integer id) {
        LsTreaty treaty = treatyService.getOne(new QueryWrapper<LsTreaty>().eq("id", id));
        return ResponseResult.responseResult(0, treaty);
    }

    /**
     * 编辑协议
      * @param treaty
     * @return
     */
    @PutMapping("updateTreaty")
    public ResponseResult updateTreaty(@RequestBody LsTreaty treaty) {
        if (null == treaty || null == treaty.getId()) {
            return ResponseResult.responseResult(ErrCodeEnum.unknown_10001, "参数不能为空");
        }
        treaty.setUpdateTime(LocalDateTime.now());
        boolean b = treatyService.updateById(treaty);
        if (b) {
            return ResponseResult.responseResult(0);
        }
        return ResponseResult.responseResult(ErrCodeEnum.unknown_20000, null);
    }

}
